/**
 * @description: 文件描述
 * @version: 0.0.1
 * @author: xxx
 * @Date: 2024-07-29 18:15:00
 */
const mixin = require("../../mixins");
Component({
  behaviors: [mixin],
  data: {
    imgSrc: "",
    goodList: [],
  },

  properties: {
    mf_data: Object,
    customAppear: Boolean,
    type: String,
  },
  attached() {
    this.init();
    // this.getData()
  },
  timer: null,
  methods: {
	
	  
    getData() {
      // 字段名: commodityListType
      // 字段值: ao / member / draw (普通/会员/抽签
      const page = getCurrentPages();
      let {
        utoolsClassGoodsLists,
      } = page[page.length - 1].data;
      let list = utoolsClassGoodsLists;
      let {
        mf_data
      } = this.properties;
      let {
        children
      } = mf_data;
      let dealChildren = [];
      if (list) {
        for (let i = 0; i < children.length; i++) {
          const item = children[i];
          item.domId = `ballout_${item.itemId}_${item.valueId}`;
          let goods = list.filter(it => it.itemId == item.itemId)[0];
          if (goods) {
            item.drawInfo = null;
            const frontCustomParameters = goods?.frontCustomParameters ? JSON.parse(goods.frontCustomParameters) :
              null;
            if (goods.approveStatus != "onsale" || goods.activitySelection || frontCustomParameters
              ?.privateSaleMark) {
              goods.frontQty = 0;
            }
            item.frontCustomParameters = frontCustomParameters;
            item.price = goods?.price + '' || item.curPrice;
            item.title_EN = frontCustomParameters?.className || item.title_EN;
            item.mainUrl = goods?.hoverUrl || item.mainUrl;
            item.title_CH = goods?.subtitle || item.title_CH;
          } else {
            goods = {
              price: item?.curPrice,
              status: 1,
              frontQty: 0,
              itemId: item.itemId,
              valueId: item.valueId || null,
              btnText: '查看详情'
            }
            item.title_EN = item.title_EN || "";
            item.price = item.curPrice + '';
          }

          item.goods = goods;
          item.price = item.price;
          dealChildren.push(item);
        }
      }
      this.setData({
        children: dealChildren,
        dchildren: dealChildren,
      });
      page[page.length - 1].$vm.setHasMore && page[page.length - 1].$vm.setHasMore(children.length);
    },
    init() {
      const nowTime = new Date().getTime();
      // 排除非抽签购商品
      const filterList = this.properties.mf_data.children.filter(i => i.drawInfo && i.drawInfo.activitySwitch)
      const list = JSON.parse(JSON.stringify(filterList));
      list.sort((a, b) => {
        if (
          b.drawInfo.exTime.exStartTime &&
          a.drawInfo.exTime.exStartTime
        ) {
          return a.drawInfo.exTime.exStartTime - b.drawInfo.exTime.exStartTime
        }
      });
      list.forEach((i) => {
        if (
          i.drawInfo.exTime.exStartTime
        ) {
          if (
            this.DaytitleFun(nowTime) ==
            this.DaytitleFun(i.drawInfo.exTime.exStartTime)
          ) {
            i.titleTime = "今天";
          } else {
            i.titleTime = this.DaytitleFun(i.drawInfo.exTime.exStartTime);
          }
        } else {}
      });

      //  //同一天归类一起
      const result = list.reduce((acc, curr) => {
        const existingGroup = acc.find((group) => {
          if (group.children[0].titleTime === curr.titleTime) {
            return curr;
          }
        });

        curr.cardComp = this.properties.mf_data.cardComp;
        if (existingGroup) {
          existingGroup.children.push(curr);
        } else {
          acc.push({
            children: [curr],
          });
        }

        return acc;
      }, []);

      result.forEach(i => {
        let classString = []
        if (i.children.length) {
          i.children.forEach(t => {
            classString.push('to'+t.itemId)
          })
        }
        i.children[0].classString = classString.join(' ')
      })
      this.setData({
        goodList: result,
      });
    },
    DaytitleFun(timestamp) {
      const date = new Date(timestamp);
      const year = date.getFullYear();
      const month = ("0" + (date.getMonth() + 1)).slice(-2);
      const day = ("0" + date.getDate()).slice(-2);
      return `${month}月${day}日`;
    },

  },
});
